package net.sf.midollo.io.log;

import net.sf.midollo.io.DefaultIOStream;
import net.sf.midollo.io.IOStream;
import net.sf.midollo.io.IOStreamFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class LogIOStreamFactory implements IOStreamFactory {

	protected final IOStreamFactory ioStreamFactory;
	protected final OutputStream logInput;
	protected final OutputStream logOutput;

	public LogIOStreamFactory(IOStreamFactory ioStreamFactory, OutputStream logInput, OutputStream logOutput) {
		this.ioStreamFactory = ioStreamFactory;
		this.logInput = logInput;
		this.logOutput = logOutput;
	}

	public IOStream open() throws IOException {
		IOStream ioStream = this.ioStreamFactory.open();
		InputStream is = ioStream.getInput();
		if (this.logInput != null)
			is = new LogInputStream(is, this.logInput);
		OutputStream os = ioStream.getOutput();
		if (this.logOutput != null)
			os = new LogOutputStream(os, this.logOutput);
		return new DefaultIOStream(is, os);
	}
}
